local buffLib = {}
package.path = package.path ..';../?.lua'
local tool = require('lib/tool')

local buffManager = tool.class('buffManager')

function buffManager:Ctor()
    self.tbBuffs = {}

end

function buffManager:addBuff(buff)

    table.insert(self.tbBuffs,buff)
    buff.index = #self.tbBuffs
    buff:setManager(self)

end

function buffManager:removeBuff(buff)
    table.remove(self.tbBuffs,buff.index)
end

function buffManager:GetBuffCount()
    return #self.tbBuffs
end

function buffManager:getEffectIndex(aBuff)
    local relationWeightMax = 0
    local effectIndex = nil
    for i,buff in ipairs(self.tbBuffs) do
        local relationWeight = buffManager:getRelationWight(aBuff,buff)
        if relationWeight >= relationWeightMax then
            relationWeightMax = relationWeight
            effectIndex = i
        end
    end
    return effectIndex
end

function buffManager:getRelationWight(buff1,buff2)
    local relationWeight = 0
    if buff1:GetGroupID() == buff2:GetGroupID() then
        relationWeight = relationWeight + 4
    end
    if buff1.dwBuffID == buff2.dwBuffID then
        relationWeight = relationWeight + 2
    end
    if buff1.dwCasterID == buff2.dwCasterID then
        relationWeight = relationWeight + 1
    end

    return relationWeight
end

function buffManager:addBuffWithRule(aBuff, dwRule, index)
    local targetBuff = self.tbBuffs[index]
    if buffLib.BuffChangeRulecfg.NotToReplace == dwRule then
        return
    end
    if buffLib.BuffChangeRulecfg.TimeAdd == dwRule then
        targetBuff:setLiveTime(aBuff:getLiveTime()+targetBuff:getLiveTime())
        return
    end
    if buffLib.BuffChangeRulecfg.LevelReplace == dwRule then
        if aBuff:getLevel() > targetBuff:getLevel() then
            targetBuff:End()
            self:addBuff(aBuff)
        end
        return
    end
    if buffLib.BuffChangeRulecfg.ValueAdd == dwRule then
        if aBuff:getLiveTime() > targetBuff:getLiveTime() then
            targetBuff:setLiveTime(aBuff:getLiveTime())
            targetBuff:setEffectTimes(aBuff:getEffectTimes() + targetBuff:getEffectTimes())
        end
        return
    end
    if buffLib.BuffChangeRulecfg.BuffAdd == dwRule then
        self:addBuff(aBuff)
        return
    end
end

local buff = tool.class('buff')

function buff:Ctor(dwBuffID, dwCasterID, dwLifeTime, dwEffectTimes)
    self.dwBuffID = dwBuffID
    self.dwCasterID = dwCasterID
    self.dwLifeTime = dwLifeTime
    self.dwEffectTimes = dwEffectTimes

end

function buff:GetGroupID()
   if self.dwGroupID then
       return self.dwGroupID
   else
       self.dwGroupID = math.floor(self.dwBuffID/100)
       return self.dwGroupID
   end
end

function buff:getLevel()
    if self.dwLevel then
        return self.dwLevel
    else
        self.dwLevel = self.dwBuffID%100
        return self.dwLevel
    end
end

function buff:End()
    if self.manager then
        self.manager:removeBuff(self)
    end
end

function buff:setManager(aManager)
    assert(aManager.removeBuff ~= nil)
    self.manager = aManager
end

function buff:getLiveTime()
    return self.dwLifeTime
end

function buff:setLiveTime(value)
    self.dwLifeTime = value
end

function buff:getEffectTimes()
    return self.dwEffectTimes
end

function buff:setEffectTimes(value)
    self.dwEffectTimes = value
end

local BuffManagerConfig =
{
	dwPMaxBuff = 20;
	dwMMaxbuff = 10;

}
local BuffChangeRulecfg =
{
	NotToReplace = 0;	--不可替换
	LevelReplace = 1; 	--等级替换
	TimeAdd = 2;		--时间叠加
	ValueAdd = 3;		--层级叠加
	BuffAdd = 4;		--并存
}

buffLib.BuffManagerConfig = BuffManagerConfig
buffLib.BuffChangeRulecfg = BuffChangeRulecfg
buffLib.buffManager = buffManager
buffLib.buff = buff

return buffLib
